/**
 * 自定义函数
 * */


//列表
function getIndex(){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'qr/list',
        data: {},
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formList = res.data;
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i, qrlink;
    h += '';
    for (i = 0; i < d.length; i ++) {
        qrlink = d[i].qrlink;
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>';
        h += d[i].code;
        h += '<br/>';
        h += '<a href="javascript:;" style="color: #0088CC;" title="点击显示二维码：' + qrlink + '" onclick="popQr(' + d[i].id + ');">点击看码</a>';
        h += '<input type="hidden" id="qrlink-' + d[i].id + '" value="' + qrlink + '"/>';
        h += '<input type="hidden" id="showCode-' + d[i].id + '" value="' + d[i].show_code + '"/>';
        h += '</td>';
        h += '<td>' + d[i].show_code + '</td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td>' + d[i].usetime + '</td>';
        h += '<td>' + d[i].type_name + '</td>';
        h += '<td>' + d[i].yw_id + '</td>';
        h += '<td><a href="qrlog.html?code=' + d[i].code + '" style="color: #0088CC;">' + d[i].scan_total + '</a></td>';
        h += '</tr>';
    }
    return h;
}


//二维码页面-生成二维码
function popQr(v){
    var id = v;
    var qrlink = $('#qrlink-' + v).val();
    var showCode = $('#showCode-' + v).val();
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var cwBody = 220, chBody = 240;
    var cwTop = (ch - chBody) / 2 - 50, chLeft = (cw - cwBody) / 2;
    var h = '', i;
    h += '<style type="text/css">';
    h += '#qrMask{width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.5; position: fixed; top: 0; left: 0; z-index: 100;}';
    h += '#qrDiv{padding: 10px; width: ' + cwBody + 'px; height: ' + chBody + 'px; background: #FFF; border: 1px solid #555; overflow: hidden; position: fixed; top: '+cwTop+'px; left: '+chLeft+'px; z-index: 100;}';
    h += '.qrOne{margin-bottom: 10px; padding: 10px; width: 235px; height: 260px; float: left; position: relative;}';
    h += '.qrBox{width: 200px; height: 200px;}';
    h += '.qrtxt{padding-top: 5px; width: 200px; text-align: center; color: #000; font-weight: bolder; font-size: 18px;}';
    h += '#qrBtn{padding: 10px 1px; width: '+ (cwBody + 20) +'px; text-align: center; background: #fff; color: #333; cursor: pointer; position: fixed; top: ' + (cwTop + 280 ) + 'px; left: ' + (cw - cwBody) / 2 +'px; z-index: 100;}';
    h += '#qrBtn:hover{background: #ddd;}';
    h += '</style>';
    h += '<div id="qrMask" onclick="$(\'#qrHtml\').html(\'\');"></div>';
    h += '<div id="qrDiv">';
    h += '<div class="qrOne">';
    //h += '<div class="qrtitle">扫码有惊喜</div>';
    h += '<div class="qrBox" title="' + qrlink + '" id="qr' + id + '"></div>';
    h += '<div class="qrtxt">' + showCode + '</div>';
    h += '</div>';
    h += '</div>';
    h += '<div id="qrBtn" onclick="downQr(' + id + ');" title="' + qrlink + '"><b>点击下载二维码</b></div>';
    h += '<input type="hidden" id="showCode' + id + '" value="' + showCode + '"/>';
    $('#qrHtml').html(h);
    $('#qr'+id).qrcode({
        render: 'canvas', text: qrlink,
        width: 200, height: 200,
        background: "#fff", //背景颜色
        foreground: "#000", //前景颜色
        src: '/admin/imgs/baim.png'
    });
}


//点击二维码下载图片
function downQr(id){
    var ele = document.getElementById('qrDiv'),
        showCode = $('#showCode' + id).val();
    html2canvas(ele,{
        useCORS: true,
        sale: 1,
    }).then(function(canvas){
        canvas.toBlob(function(blob){
            saveAs(blob, showCode + '.png');
        });
    });
}


//修改
function add(){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'qr/add',
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            //window.location.href = '';
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//列表
function qrlist(){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'qrlog/list',
        data: { code: code },
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formList = res.data;
            tbody.html(qrHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function qrHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].code + '</td>';
        h += '<td>' + d[i].show_code + '</td>';
        h += '<td>' + d[i].uname + '</td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '</tr>';
    }
    return h;
}